<?php 
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../DAO/MemberDAO.php';
class MemberService
{
	public function create(BaseContext $context, ServiceResult $result, MemberDomain $memberDomain)
	{
		try
		{
			$memberDAO = new MemberDAO($context);
			$memberDAO->create($memberDomain);
			$result->setSuccess();
		}
		catch (Exception $e)
		{
			$context->logException($e);
			$result->setException($e);
		}
	}

	public function update(BaseContext $context, ServiceResult $result, MemberDomain $memberDomain)
	{
		try
		{
			$memberDAO = new MemberDAO($context);
			$memberDAO->update($memberDomain);
			$result->setSuccess();
		}
		catch (Exception $e)
		{
			$context->logException($e);
			$result->setException($e);
		}
	}

	public function delete(BaseContext $context, ServiceResult $result, $oid)
	{
		try
		{
			$memberDAO = new MemberDAO($context);
			$memberDAO->delete($oid);
			$result->setSuccess();
		}
		catch (Exception $e)
		{
			$context->logException($e);
			$result->setException($e);
		}
	}

	public function findByOid(BaseContext $context, ServiceResult $result, $oid)
	{
		try
		{
			$memberDAO = new MemberDAO($context);
			$memberDomain = $memberDAO->findByOid($oid);
			$result->setSuccess();
			return $memberDomain;
		}
		catch (Exception $e)
		{
			$context->logException($e);
			$result->setException($e);
		}
	}

	public function findAll(BaseContext $context, ServiceResult $result)
	{
		try
		{
			$memberDAO = new MemberDAO($context);
			$memberDomainArr = $memberDAO->findAll();
			$result->setSuccess();
			return $memberDomainArr;
		}
		catch (Exception $e)
		{
			$context->logException($e);
			$result->setException($e);
		}
	}
	
	public function login (BaseContext $context, ServiceResult $result, $email, $password)
	{
		try 
		{			
			$memberDao = new MemberDAO($context);	
			$password = Tool::md5_base64($password);					
			$memberDomain = $memberDao->findByEmailPass($email, $password);	
			if($memberDomain != null)
			{
				return $memberDomain;
			}
			$result->setSuccess();
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
		}
		return false;
	}

}
?>